package com.td.module.equipment.framework.core;

import cn.hutool.http.HttpUtil;
import com.td.framework.common.util.json.JsonUtils;
import com.td.module.equipment.framework.config.ZhyProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ZhyClient {

    @Resource
    private ZhyProperties zhyProperties;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private HashMap<String, Object> request(HashMap<String, Object> params, String uri) {

        try {

            String responseText = HttpUtil.post(zhyProperties.getUrl() + uri, params);

            HashMap<String, Object> responseObj = JsonUtils.parseObject(responseText, HashMap.class);

            if (Objects.equals(0, responseObj.get("code"))) {
                return responseObj;
            } else {
                log.info("[智汇耘接口异常]", responseObj);
                return null;
            }

        } catch (Exception e) {
            log.info("[智汇耘连接异常]", e);
            return null;
        }

    }

    /**
     * 设备列表接口
     * @return
     * @throws Throwable
     */
    public HashMap<String, Object> login() {

        String cacheKey = "zhy.login";
        HashMap<String, Object> loginData = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(cacheKey), HashMap.class);
        if (loginData == null) {

            HashMap<String, Object> params = new HashMap<>();
            params.put("phone_num", zhyProperties.getPhoneNum());
            params.put("password", zhyProperties.getPassword());

            HashMap<String, Object> loginObject = request(params, "/zhy/login");
            loginData = (HashMap<String, Object>) loginObject.get("data");
            if (Objects.equals(0, loginObject.get("code"))) {
                stringRedisTemplate.opsForValue().set(cacheKey, JsonUtils.toJsonString(loginData), 43200L, TimeUnit.SECONDS);
            }

        }

        return loginData;

    }

    public List<HashMap<String, Object>> deviceList(int pageIndex, int pageSize) {

        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", loginData.get("user_id"));
        params.put("user_token", loginData.get("user_token"));
        params.put("page_index", pageIndex);
        params.put("page_size", pageSize);

        return (List<HashMap<String, Object>>) request(params, "/zhy/device/device_list").get("data");
    }

    /**
     * 设备下发指令（设备控制）接口
     * @param equipmentId
     * @param cmd
     * @return
     * @throws Throwable
     */
    public HashMap<String, Object> sendCmd(String equipmentId, int cmd) {

        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", loginData.get("user_id"));
        params.put("user_token", loginData.get("user_token"));
        params.put("equipment_id", equipmentId);
        params.put("cmd", cmd);

        return request(params, "/zhy/device/send_cmd");
    }

    /**
     * 轮询设备下发指令状态接口
     * @param uuid
     * @return
     * @throws Throwable
     */
    public HashMap<String, Object> pollingResult(String uuid) {

        HashMap<String, Object> loginData = login();
        if (loginData == null) {
            return null;
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", loginData.get("user_id"));
        params.put("user_token", loginData.get("user_token"));
        params.put("uuid", uuid);

        return request(params, "/zhy/roll/polling_result");
    }


}
